package com.bl.weilan.service.biz.companymodel.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyModelEntity;
import com.bl.weilan.dao.repository.CompanyModelRepository;
import com.bl.weilan.service.biz.companymodel.CompanyModelService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companymodel.CompanyModelAddVo;
import com.bl.weilan.service.vo.companymodel.CompanyModelListVo;
import com.bl.weilan.service.vo.companymodel.CompanyModelModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class CompanyModelServiceImpl implements CompanyModelService {
    @Autowired
    CompanyModelRepository companymodelRepository;

    @Override
    public CompanyModelEntity getCompanyModelById(String companymodelId) {
        CompanyModelEntity companymodelEntity = companymodelRepository.findOne(companymodelId);
        return companymodelEntity;
    }

    @Override
    public CompanyModelListVo getCompanyModelLists(Pageable pageable ){
        Page<CompanyModelEntity> companymodelEntities;
        CompanyModelListVo companymodelListVo = new CompanyModelListVo();

        companymodelEntities = companymodelRepository.findAll(pageable);


        companymodelListVo.setTotalCount(companymodelEntities.getTotalElements());
        companymodelListVo.setList(companymodelEntities.getContent());

        return companymodelListVo;
    }

    @Override
    public void addCompanyModel(CompanyModelAddVo companymodelAddVo) {
        CompanyModelEntity companymodelEntity = new CompanyModelEntity();
        companymodelEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companymodelAddVo, companymodelEntity);
        companymodelRepository.save(companymodelEntity);

    }

    @Override
    public void del(DelBody id) {
        companymodelRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            companymodelRepository.delete(id);
        }
    }

    @Override
    public void modify(CompanyModelModVo companymodelModVo){
        CompanyModelEntity companymodelEntity = companymodelRepository.findOne(companymodelModVo.getId());

        if (companymodelEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(companymodelModVo, companymodelEntity);
        companymodelRepository.save(companymodelEntity);
    }
}
